//
//  AccountverifyViewController.m
//  GC_OC
//
//  Created by Tianbao Wang on 2022/5/27.
//

#import "AccountverifyViewController.h"
#import "LoginTextfieldView.h"
#import "PwdVerifyViewController.h"

@interface AccountverifyViewController ()<UITextFieldDelegate>
@property(nonatomic, strong) UIView *navigationBarView;
@property(nonatomic, strong) LoginTextfieldView *accountView;
@property(nonatomic, strong) LoginTextfieldView *yzView;
@property(nonatomic, strong) LoginTextfieldView *daView;
@property (nonatomic, strong) ObjectAFNetWorking* objectAFNet;
@property (nonatomic, strong) NSDictionary* verificationDic;

@end

@implementation AccountverifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.objectAFNet = [ObjectAFNetWorking new];
    [self setupUIView];
    [self refreshYz];
}

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:true animated:true];
//    
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    [self.navigationController setNavigationBarHidden:false animated:true];
//    
//}

- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)doneAct {
    [self downkeyborad];
    if ([self.accountView.textfield.text length] == 0) {
        [self.view makeToast:@"请输入账号"];
        return;
    }
    if ([self.daView.textfield.text length] == 0) {
        [self.view makeToast:@"请输入答案"];
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.objectAFNet postUrlStr:[NSString stringWithFormat:@"%@/api/user/resetUser",server_addr] parames:@{@"username":self.accountView.textfield.text,@"code":self.verificationDic[@"code"],@"verification_answer":self.daView.textfield.text} finish:^(id  _Nonnull dataString) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        if ([dic[@"code"] isEqual:@200]) {
            PwdVerifyViewController *control = [PwdVerifyViewController new];
            control.account = self.accountView.textfield.text;
            [self.navigationController pushViewController:control animated:YES];
        } else {
            [self.view makeToast:dic[@"msg"]];
        }
        
    } andError:^(NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];
}

//获取随机验证码
- (void)refreshYz {
    [self.objectAFNet getUrlStr:[NSString stringWithFormat:@"%@/api/user/verification",server_addr] finish:^(id  _Nonnull dataString) {
        NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        if ([dic[@"code"] isEqual:@1]) {
            NSDictionary *data = dic[@"data"];
            self.verificationDic = data;
            self.yzView.textfield.text = data[@"title"];
        }
    } andError:^(NSError * _Nonnull error) {
        
    }];
}

- (void)setupUIView {
    self.view.backgroundColor = UIColor.whiteColor;
    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_bg"]];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.navigationBarView = [UIView new];
    self.navigationBarView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.navigationBarView];
    [self.navigationBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(NAVI_BAR_HIGHT);
    }];
    
    UILabel *naviTitle = [UILabel new];
    naviTitle.text = @"账号验证";
    naviTitle.textAlignment = NSTextAlignmentCenter;
    naviTitle.textColor = [UIColor blackColor];
    naviTitle.font = [UIFont systemFontOfSize:20 weight:UIFontWeightMedium];
    [self.navigationBarView addSubview:naviTitle];
    [naviTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.navigationBarView);
        make.top.mas_equalTo(StatusBarHeight);
        make.height.mas_equalTo(40);
    }];
    
    UIButton *backButton = [UIButton new];
    [backButton setImage:[UIImage imageNamed:@"icon_返回_32_left"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationBarView addSubview:backButton];
    [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(32);
        make.left.equalTo(self.view).offset(12);
        make.top.mas_equalTo(StatusBarHeight + 4);
    }];
    
    UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
    [self.view addSubview:logoView];
    [logoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(160);
    }];
    
    self.accountView = [[LoginTextfieldView alloc] init];
    self.accountView.nameLabel.text = @"账号：";
    self.accountView.layer.borderWidth = 0;
    [self.view addSubview:self.accountView];
    [self.accountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(logoView.mas_bottom).offset(72);
        make.height.mas_equalTo(34);
    }];
    
    self.yzView = [[LoginTextfieldView alloc] init];
    self.yzView.nameLabel.text = @"验证：";
    self.yzView.textfield.layer.borderWidth = 0;
    self.yzView.textfield.userInteractionEnabled = NO;
    [self.view addSubview:self.yzView];
    [self.yzView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.accountView.mas_bottom).offset(15);
        make.height.mas_equalTo(34);
    }];
    
    self.daView = [[LoginTextfieldView alloc] init];
    self.daView.nameLabel.text = @"答案：";
    [self.view addSubview:self.daView];
    [self.daView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.yzView.mas_bottom).offset(15);
        make.height.mas_equalTo(34);
    }];
    
    UIButton *doneButton = [UIButton new];
    doneButton.clipsToBounds = YES;
    doneButton.layer.cornerRadius = 28;
    doneButton.backgroundColor = XHUIColorFromRGB(0x0091FF);
    [doneButton setTitle:@"验证" forState:UIControlStateNormal];
    doneButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC" size: 16];
    [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(doneAct) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:doneButton];
    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(56);
        make.right.equalTo(self.view).offset(-50);
        make.left.equalTo(self.view).offset(50);
        make.top.equalTo(self.daView.mas_bottom).offset(20);
    }];
}

- (void)downkeyborad {
    [self.accountView.textfield resignFirstResponder];
    [self.daView.textfield resignFirstResponder];
}
@end
